﻿namespace MefContrib.Models.Provider.Tests
{
    using System.Reflection;
    using NUnit.Framework;
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Primitives;

    [TestFixture]
    public class MemberInfoExtensionsTests
    {
        [Test]
        public void ToComposableMemberShouldReturnComposableMemberInstance()
        {
            MemberInfo member =
                typeof(FakePropertyPart).GetMember("NonStaticReadWrite")[0];

            ComposableMember composable =
                member.ToComposableMember();

            Assert.IsNotNull(composable);
        }

        [Test, ExpectedException(typeof(ImportCardinalityMismatchException))]
        public void GetCardinalityShouldThrowExceptionIfNotCalledOnPropertyOrField()
        {
            MemberInfo member =
                typeof(FakeMethodPart).GetMember("NoReturnNoParameters")[0];

            member.GetCardinality(false);
        }

        [Test]
        public void GetCardinalityShouldReturnZeroOrMoreOnEnumerableMemberAndAllowDefaultIsFalse()
        {
            ComposableProperty composable =
                new ComposableProperty(typeof(FakePropertyPart).GetMember("NonLazyCollection")[0]);

            Assert.IsTrue(composable.Property.GetCardinality(false) == ImportCardinality.ZeroOrMore);
        }

        [Test]
        public void GetCardinalityShouldReturnZeroOrMoreOnEnumerableMemberAndAllowDefaultIsTrue()
        {
            ComposableProperty composable =
                new ComposableProperty(typeof(FakePropertyPart).GetMember("NonLazyCollection")[0]);

            Assert.IsTrue(composable.Property.GetCardinality(true) == ImportCardinality.ZeroOrMore);
        }

        [Test]
        public void GetCardinalityShouldReturnExactlyOneOnNonEnumerableMemberAndAllowDefaultFalse()
        {
            MemberInfo member =
               typeof(FakePropertyPart).GetMember("NonStaticReadWrite")[0];

            Assert.IsTrue(member.GetCardinality(false) == ImportCardinality.ExactlyOne);
        }

        [Test]
        public void GetCardinalityShouldReturnZeroOrOneOnNonEnumerableMemberAndAllowDefaultTrue()
        {
            MemberInfo member =
               typeof(FakePropertyPart).GetMember("NonStaticReadWrite")[0];

            Assert.IsTrue(member.GetCardinality(true) == ImportCardinality.ZeroOrOne);
        }
    }
}